$(function() {
	// console.log("OK");
	var page = 1;
	var allCount = 0;
	var curPage = 1;
	var allPage = 1;
	var count = 10;
	var searchVal = "";
	// getData();
	// getAllPage();
	// getPageData(page);
	getSearchUser(searchVal, page, count)

	$(".searchBtn").click(function() {
		searchVal = $(".search").val() || ""
		getSearchUser(searchVal, page, count);
	});

	$("#myTab").click(function(e) {
		var ev = e || window.event;
		var tar = ev.target;
		var tarp = $(tar).parent();
		var tarpi = $.trim(tarp[0].innerText);
		var tarpp = $(tarp).parent()[0];
		var index = tarpp.rowIndex;
		var da = tarpp.innerText.split("	");
		var val = tar.getAttribute("data-value");
		var that = this;
		if (tarpi == "修改") {
			console.log("--修改");
			var sex;
			if (da[5] == "男") {
				$("input[type='radio'][value=0]").attr("checked", false);
				$("input[type='radio'][value=1]").attr("checked", true);
			} else {
				$("input[type='radio'][value=1]").attr("checked", false);
				$("input[type='radio'][value=0]").attr("checked", true);
			}
			$("input[name='editId']").val(da[0]);
			$("input[name='editName']").val(da[1]);
			$("input[name='editPasswd']").val(da[2]);
			$("input[name='editCompany']").val(da[3]);
			$("input[name='editAge']").val(da[4]);

			$('#editModal').modal('show');
		} else if (tarpi == "添加") {
			$('#addModal').modal('show');
		} else if (tarpi == "删除") {
			console.log("--删除");
			var id = da[0];
			if (confirm("确认删除该条id为：" + id + " 的数据")) {
				delUser(id);
				// getData();
				// getPageData(page);
				// getAllPage();
				searchVal = $(".search").val() || ""
				getSearchUser(searchVal, page, count)
			} else
				return;
		} else if (tarpi == '+') {
			if ($(".t_add").length < 1) {
				// var tb = document.getElementById("table1");
				var newTr = this.insertRow(index + 1); //表示在第二行后添加一行
				newTr.className = 't_add';
				// var newTd = newTr.insertCell(); //表示在添加的行上添加第一格
				newTr.insertCell().innerHTML = "<input type='text' class='plusInput' name='t_addId'>";
				newTr.insertCell().innerHTML = "<input type='text' class='plusInput' name='t_addName'>";
				newTr.insertCell().innerHTML = "<input type='text' class='plusInput' name='t_addPasswd'>";
				newTr.insertCell().innerHTML = "<input type='text' class='plusInput' name='t_addCompany'>";
				newTr.insertCell().innerHTML = "<input type='text' class='plusInput' name='t_addAge'>";
				newTr.insertCell().innerHTML = "<label>男<input type='radio' checked name='t_addSex' value='1'></label><label>女<input type='radio' name='t_addSex' value='0'></label>";
				newTr.insertCell().innerHTML = "<b class='glyphicon glyphicon-ok save' data-value='1'></b>";
				newTr.insertCell().innerHTML = "<b class='glyphicon glyphicon-refresh reset' data-value='0'></b>";
				newTr.insertCell().innerHTML = "<b class='glyphicon glyphicon-remove remove' data-value='-1'></b>";
			} else {
				alert("已经存在一行添加,请先完成！");
				return;
			}
		} else if (val == '1') { //提交
			var userName = $("input[name='t_addName']").val();
			var userId = $("input[name='t_addId']").val();
			var passwd = $("input[name='t_addPasswd']").val();
			var company = $("input[name='t_addCompany']").val();
			var age = $("input[name='t_addAge']").val();
			var sex = $("input[name='t_addSex']:checked").val();
			var url = "http://127.0.0.1:8080/friend/webuser/addUser";
			console.log("info: ", userName, " : ", userId, " : ", passwd, " : ", company, " : ", age, " : ", sex, " : ", url);
			operationUser(userName, userId, passwd, company, age, sex, url);
			// getData();
			// getPageData(page);
			// getAllPage();
			searchVal = $(".search").val() || ""
			getSearchUser(searchVal, page, count)
		} else if (val == '0') {
			console.log("000000");
			$("input[name='t_addId']").val("");
			$("input[name='t_addName']").val("");
			$("input[name='t_addPasswd']").val("");
			$("input[name='t_addCompany']").val("");
			$("input[name='t_addAge']").val("");
			$("input[name='t_addSex'][value=1]").attr("checked", true);
			$("input[name='t_addSex'][value=0]").attr("checked", false);
		} else if (val == '-1') {
			this.deleteRow(index);
		} else {
			return;
		}
	});

	$("#addSubmit").click(function(event) {
		/* Act on the event */
		var userId = $("input[name='addId']").val();
		var userName = $("input[name='addName']").val();
		var passwd = $("input[name='addPasswd']").val();
		var company = $("input[name='addCompany']").val();
		var age = $("input[name='addAge']").val();
		var sex = $("input[name='addSex']:checked").val();
		var url = "http://127.0.0.1:8080/friend/webuser/addUser";
		console.log(userName, " : ", userId, " : ", passwd, " : ", company, " : ", age, " : ", sex, " : ", url);
		operationUser(userName, userId, passwd, company, age, sex, url); //连接后端，修改数据库
		// getData();
		// getPageData(page);
		// getAllPage();
		searchVal = $(".search").val() || ""
		getSearchUser(searchVal, page, count)
		$('#addModal').modal('hide');
	});

	$(".editBtn").click(function(event) {
		/* Act on the event */
		var userName = $("input[name='editName']").val();
		var userId = $("input[name='editId']").val();
		var passwd = $("input[name='editPasswd']").val();
		var company = $("input[name='editCompany']").val();
		var age = $("input[name='editAge']").val();
		var sex = $("input[name='editSex']:checked").val();
		var url = "http://127.0.0.1:8080/friend/webuser/updateUser";

		operationUser(userName, userId, passwd, company, age, sex, url);
		$('#editModal').modal('hide');
		// getData();
		// getPageData(page);
		// getAllPage();
		searchVal = $(".search").val() || ""
		getSearchUser(searchVal, page, count)
	});

	$(".first").click(function(event) {
		/* Act on the event */
		page = 1;
		// getPageData(page);
		// getAllPage();
		searchVal = $(".search").val() || ""
		getSearchUser(searchVal, page, count)
		$(".nowPage").val(page);
	});

	$(".pre").click(function(event) {
		/* Act on the event */
		if (page <= 1) {
			alert("已经是第一页了！");
			return;
		} else {
			page--;
			// getPageData(page);
			searchVal = $(".search").val() || ""
			getSearchUser(searchVal, page, count);
		}
		$(".nowPage").val(page);
	});

	$(".next").click(function(event) {
		/* Act on the event */
		if (page >= allPage) {
			alert("已经是最后一页了！");
			return;
		} else {
			page++;
			// getPageData(page);
			searchVal = $(".search").val() || ""
			getSearchUser(searchVal, page, count)
		}
		$(".nowPage").val(page);
	});

	$(".last").click(function(event) {
		/* Act on the event */
		page = allPage;
		// getPageData(page);
		searchVal = $(".search").val() || ""
		getSearchUser(searchVal, page, count)
		$(".nowPage").val(page);
	});

	$(".toBtn").click(function(event) {
		/* Act on the event */
		var toPage = $(".toPage").val();
		if (toPage <= 0) {
			alert("页数不小于1！");
			return;
		} else if (toPage > allPage) {
			alert("页数不能大于最大页数！" + allCount);
			return;
		} else {
			page = toPage;
			// getPageData(page);
			searchVal = $(".search").val() || ""
			getSearchUser(searchVal, page, count)
		}
	});

	function getData() {
		$.ajax({
			url: "http://127.0.0.1:8080/friend/webuser/getAllUser",
			method: "post",
			dataType: 'json',
			data: {},
			success: function(res) {
				console.log("post res: ", res);
				var _html = "";
				var data = res.data2;
				// insertHtml(data, queryUser);
				for (var i = 0; i < data.length; i++) {
					var inHtml = "<tr>";
					inHtml += "<td>" + data[i].userid + "</td>";
					inHtml += "<td>" + data[i].username + "</td>";
					inHtml += "<td>" + data[i].password + "</td>";
					inHtml += "<td>" + data[i].company + "</td>";
					inHtml += "<td>" + data[i].age + "</td>";
					inHtml += "<td>" + data[i].sex + "</td>";
					inHtml += "<td><b class='edit btn'  data-target='#editModal'>修改</b></td>";
					inHtml += "<td><b class='add btn'  data-target='#addModal'>添加</b></td>";
					inHtml += "<td><b class='del btn' data-target='#delModal'>删除</b></td>";
					inHtml += "<td class='plus'><b>+</b></td></tr>";
					_html += inHtml;
				}
				// document.getElementById("tb").innerHTML = _html;
				$("#tb").html(_html);
			},
			error: function(error) {
				console.log("post error: ", error)
			}
		});
	}

	function getAllPage() {
		$.ajax({
			url: 'http://127.0.0.1:8080/friend/webuser/getAllPageCount',
			method: 'post',
			dataType: 'json',
			success: function(res) {
				console.log("allCount: ", res);
				allCount = res.pageCount;
				allPage = Math.ceil(allCount / count);
				$(".allCount").text(allCount);
				$(".allPage").text(allPage);
			},
			error: function(error) {
				console.log("error: ", error);
			}
		});
	}


	function getPageData(page) {
		$.ajax({
			url: 'http://127.0.0.1:8080/friend/webuser/getPageUser',
			method: 'post',
			dataType: 'json',
			data: {
				num: page,
				count: count
			},
			success: function(res) {
				console.log("allCount: ", res);
				var _html = "";
				var data = res.data2;
				// insertHtml(data, queryUser);
				for (var i = 0; i < data.length; i++) {
					var inHtml = "<tr>";
					inHtml += "<td>" + data[i].id + "</td>";
					inHtml += "<td>" + data[i].username + "</td>";
					inHtml += "<td>" + data[i].password + "</td>";
					inHtml += "<td>" + data[i].company + "</td>";
					inHtml += "<td>" + data[i].age + "</td>";
					inHtml += "<td>" + data[i].sex + "</td>";
					inHtml += "<td><b class='edit btn'  data-target='#editModal'>修改</b></td>";
					inHtml += "<td><b class='add btn'  data-target='#addModal'>添加</b></td>";
					inHtml += "<td><b class='del btn' data-target='#delModal'>删除</b></td>";
					inHtml += "<td class='plus'><b>+</b></td></tr>";
					_html += inHtml;
				}
				// document.getElementById("tb").innerHTML = _html;
				$("#tb").html(_html);
			},
			error: function(error) {}
		});
	}

	function getSearchUser(searchVal, page, count) {
		$.ajax({
			url: 'http://127.0.0.1:8080/friend/webuser/getSearchUser',
			method: 'post',
			dataType: 'json',
			data: {
				searchVal: searchVal,
				pageNum: page,
				count: count
			},
			success: function(res) {
				console.log("allCount: ", res);
				var _html = "";
				var data = res.data;
				allCount = res.count;
				allPage = Math.ceil(allCount / count);
				$(".allCount").text(allCount);
				$(".allPage").text(allPage);
				$("#tb").html("");
				// insertHtml(data, queryUser);
				for (var i = 0; i < data.length; i++) {
					var inHtml = "<tr>";
					inHtml += "<td>" + data[i].id + "</td>";
					inHtml += "<td>" + data[i].username + "</td>";
					inHtml += "<td>" + data[i].password + "</td>";
					inHtml += "<td>" + data[i].company + "</td>";
					inHtml += "<td>" + data[i].age + "</td>";
					inHtml += "<td>" + data[i].sex + "</td>";
					inHtml += "<td><b class='edit btn'  data-target='#editModal'>修改</b></td>";
					inHtml += "<td><b class='add btn'  data-target='#addModal'>添加</b></td>";
					inHtml += "<td><b class='del btn' data-target='#delModal'>删除</b></td>";
					inHtml += "<td class='plus'><b>+</b></td></tr>";
					_html += inHtml;
				}
				// document.getElementById("tb").innerHTML = _html;
				$("#tb").html(_html);
			},
			error: function(error) {}
		});
	}

	function operationUser(userName, userId, passwd, company, age, sex, url) {
		$.ajax({
			url: url,
			method: "post",
			dataType: 'json',
			async: false,
			data: {
				"id": userId,
				"username": userName,
				"password": passwd,
				"company": company,
				"age": age,
				"sex": sex
			},
			success: function(res) {
				console.log("post res: ", res);
				alert(res.msg);
			},
			error: function(error) {
				console.log("post error: ", error);
			}
		});
	}

	function delUser(userid) {
		$.ajax({
			url: "http://127.0.0.1:8080/friend/webuser/delUser",
			method: "post",
			dataType: 'json',
			async: false,
			data: {
				"id": userid
			},
			success: function(res) {
				// alert(res.msg);
				console.log("post res: ", res);
			},
			error: function(error) {
				console.log("post error: ", error)
			}
		});
	}
});